package com.zhentao.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.TMenu;
import com.zhentao.domain.TRoleMenu;
import com.zhentao.domain.dto.AdMenu;
import com.zhentao.mapper.TRoleMenuMapper;
import com.zhentao.result.Result;
import com.zhentao.service.TMenuService;
import com.zhentao.mapper.TMenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author DELL
* @description 针对表【t_menu】的数据库操作Service实现
* @createDate 2024-08-12 18:38:25
*/
@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu>
    implements TMenuService{

    @Resource
    private TMenuMapper menuMapper;

   @Resource
   private TRoleMenuMapper roleMenuMapper;


    @Override
    public Result getMenuList(String menuName) {
        QueryWrapper<TMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TMenu::getParentId, 0);
        if (menuName != null && !menuName.equals("")) {
            queryWrapper.lambda().like(TMenu::getMenuName,menuName);
        }
        List<TMenu> tMenus = this.menuMapper.selectList(queryWrapper);
        tMenus.forEach(menu->{
            QueryWrapper<TRoleMenu> queryWrapperMenu=new QueryWrapper<>();
            queryWrapperMenu.lambda().eq(TRoleMenu::getMenuId,menu.getMenuId());
            List<TRoleMenu> tRoleMenus = this.roleMenuMapper.selectList(queryWrapperMenu);
            menu.setRoleIds(tRoleMenus);
            QueryWrapper<TMenu> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.lambda().eq(TMenu::getParentId,menu.getMenuId());
            if (menuName != null && !menuName.equals("")) {
                queryWrapper1.lambda().like(TMenu::getMenuName,menuName);
            }
            List<TMenu> tMenus1 = this.menuMapper.selectList(queryWrapper1);
            tMenus1.forEach(menu1->{
                QueryWrapper<TRoleMenu> queryWrapperMenu1=new QueryWrapper<>();
                queryWrapperMenu1.lambda().eq(TRoleMenu::getMenuId,menu1.getMenuId());
                List<TRoleMenu> tRoleMenus1 = this.roleMenuMapper.selectList(queryWrapperMenu1);
                menu1.setRoleIds(tRoleMenus1);
            });
            menu.setMenus(tMenus1);
        });
        return Result.OK(tMenus);
    }

    @Override
    public Result addMenu(AdMenu adMenu) {
        TMenu tMenu = new TMenu();
        tMenu.setMenuName(adMenu.getMenuName());
        tMenu.setParentId(adMenu.getParentId());
        tMenu.setUrl(adMenu.getUrl());
        tMenu.setOrderNo(0);
        tMenu.setIcon(adMenu.getIcon());
        tMenu.setPerms(adMenu.getPerms());
        this.menuMapper.insert(tMenu);
        Integer[] roleIds = adMenu.getRoleIds();
        for (Integer roleId : roleIds) {
            TRoleMenu tRoleMenu = new TRoleMenu();
            tRoleMenu.setMenuId(tMenu.getMenuId());
            tRoleMenu.setRoId(roleId);
            this.roleMenuMapper.insert(tRoleMenu);
        }
        return Result.OK();
    }

    @Override
    public Result upMenu(AdMenu adMenu) {
        TMenu tMenu = this.menuMapper.selectById(adMenu.getMenuId());
        tMenu.setMenuName(adMenu.getMenuName());
        tMenu.setParentId(adMenu.getParentId());
        tMenu.setUrl(adMenu.getUrl());
        tMenu.setOrderNo(0);
        tMenu.setIcon(adMenu.getIcon());
        tMenu.setPerms(tMenu.getPerms());
        this.menuMapper.updateById(tMenu);
        Integer[] roleIds = adMenu.getRoleIds();
        QueryWrapper<TRoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TRoleMenu::getMenuId,tMenu.getMenuId());
        this.roleMenuMapper.delete(queryWrapper);
        for (int i = 0; i < roleIds.length; i++) {
            TRoleMenu tRoleMenu = new TRoleMenu();
            tRoleMenu.setMenuId(tMenu.getMenuId());
            tRoleMenu.setRoId(i);
            this.roleMenuMapper.insert(tRoleMenu);
        }
        return Result.OK();
    }

    @Override
    public Result getMenuById(Integer menuId) {
        TMenu tMenu = this.menuMapper.selectById(menuId);
        QueryWrapper<TRoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TRoleMenu::getMenuId,tMenu.getMenuId());
        List<TRoleMenu> tRoleMenus = this.roleMenuMapper.selectList(queryWrapper);
        tMenu.setRoleIds(tRoleMenus);
        return Result.OK(tMenu);
    }

    @Override
    public Object delMenuById(Integer menuId) {
        this.roleMenuMapper.deleteById(menuId);
        QueryWrapper<TRoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TRoleMenu::getMenuId,menuId);
        List<TRoleMenu> tRoleMenus = this.roleMenuMapper.selectList(queryWrapper);
        int i = this.roleMenuMapper.deleteBatchIds(tRoleMenus);
        return i;
    }
}




